<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site extends CI_Controller {
    protected $data = array();
    public function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->model('menu');
        $this->load->model('page');
        $this->load->model('document');
        $this->load->model('auction');
        $this->load->model('news');
        $this->load->model('contact');
        $this->load->model('pagedocument');
    	$this->data['menu'] = $this->menu->getMenus();
        $this->data['copyright'] = '(c) Copyright 2012 ACSIS Solv I.P.U.R.L';     
    }
    public function index() {
        $page = $this->page->getPages(array('pag_homepage' => 'Y'));
        $this->pages($page['pag_name']);
    }
    
    public function pages($page, $ppage = 0) {
        $pagconf = array();
        $this->data['page'] = $this->page->getPages(array('pag_name' => $page));
        if ($this->data['page'] != false) {
            if ($this->data['page']['pag_homepage'] == 'Y') {
                $this->data['news'] = $this->news->getLast10();
                $this->data['showNews'] = $this->data['news'] != FALSE;
                if ($this->data['showNews']) {
                    foreach ($this->data['news'] as &$item) {
                        if ($item['new_type'] == 'L') {
                            $item['auction'] = $this->auction->getAuctions(array('auc_id' => $item['new_objid']));
                            if ($item['auction'] != FALSE) {
                                $item['auction']['page'] = $this->page->getPages(array('pag_id' => $item['auction']['auc_detailpage']));
                                $item['auction']['owner'] = $this->contact->getContacts(array('cnt_id' => $item['auction']['auc_owner']));
                                $item['auction']['contact'] = $this->contact->getContacts(array('cnt_id' => $item['auction']['auc_contact']));
                            }
                        } else {
                            $item['document'] = $this->document->getDocuments(array('doc_id' => $item['new_objid']));
                            if ($item['document'] != FALSE) {
                                $item['document']['page'] = $this->page->getPages(array('pag_id' => $item['document']['doc_detailpage']));
                                $item['document']['owner'] = $this->contact->getContacts(array('cnt_id' => $item['document']['doc_owner']));
                                $item['document']['contact'] = $this->contact->getContacts(array('cnt_id' => $item['document']['doc_contact']));
                            }
                        }
                    }
                }
            } else {
                $this->data['showNews'] = FALSE;
            }
            
            $this->data['page']['documents'] = $this->pagedocument->getPageDocuments(array('dp_page' => $this->data['page']['pag_id'], 'limit' => 10, 'offset' => $ppage));
            /*
            $count = 0;
            $pagdocs = $this->pagedocument->getPageDocuments(array('dp_page' => $this->data['page']['pag_id'], 'limit' => 10, 'offset' => $ppage));
            if ($pagdocs) {
                foreach ($pagdocs as $doc) {
                    $this->data['page']['documents'][$count] = $this->getDocuments(array('doc_id' => $doc['dp_document']))
                    $count++;
                }
            } else
                $this->data['page']['documents'] = FALSE;
            }
            */
            
            $this->data['page']['documentsCount'] = $this->pagedocument->countPageDocuments(array('dp_page' => $this->data['page']['pag_id']));
            $this->data['page']['hasDocuments'] = $this->data['page']['documents'] != FALSE;
            if ($this->data['page']['hasDocuments']) {
                $pagconf['documents'] = array('base_url' => base_url() . '/pages/' . $page,
                                              'total_rows' => $this->data['page']['documentsCount'],
                                              'per_page' => 10,
                                              'uri_segment' => 4);
                $this->pagination->initialize($pagconf['documents']);
                $this->data['page']['links'] = $this->pagination->create_links();
            }
            $this->data['page']['hasActiveAuctions'] = FALSE;
            $this->data['page']['hasInactiveAuctions'] = FALSE;
        } else {
            $this->data['page'] = array('pag_title' => 'Pagina solicitata nu exista. Va rugam reveniti.');
            $this->data['page']['pag_content'] = 'Pagina in constructie';
            $this->data['showNews'] = FALSE;
            $this->data['page']['hasDocuments'] = FALSE;
            $this->data['page']['hasActiveAuctions'] = FALSE;
            $this->data['page']['hasInactiveAuctions'] = FALSE;
        }
        
        $this->load->view('site_header', $this->data);
        $this->load->view('site_contenthead', $this->data);
        $this->load->view('site_contentnavi', $this->data);
        $this->load->view('site_contentnews', $this->data);
        $this->load->view('site_contentbody', $this->data);
        $this->load->view('site_contentnavi2', $this->data);
        $this->load->view('site_footer', $this->data);
    }
    
    public function documente($type, $ppage = 0) {
        $this->data['showNews'] = FALSE;
        $this->data['page'] = array();
        $this->data['page']['hasActiveAuctions'] = FALSE;
        $this->data['page']['hasInactiveAuctions'] = FALSE;
        switch($type) {
            case "insolventa":
                $this->data['page']['pag_title'] = 'Insolventa';
                $this->data['page']['pag_content'] = '';
                $this->data['page']['documents'] = $this->document->getDocuments(array('doc_type' => 'I', 'limit' => 10, 'offset' => $ppage));
                $this->data['page']['documentsCount'] = $this->document->countDocuments(array('doc_type' => 'I'));
                $this->data['page']['hasDocuments'] = $this->data['page']['documents'] != FALSE;
            break;
            case "reorganizare":
                $this->data['page']['pag_title'] = 'Reorganizare';
                $this->data['page']['pag_content'] = '';
                $this->data['page']['documents'] = $this->document->getDocuments(array('doc_type' => 'R', 'limit' => 10, 'offset' => $ppage));
                $this->data['page']['documentsCount'] = $this->document->countDocuments(array('doc_type' => 'R'));
                $this->data['page']['hasDocuments'] = $this->data['page']['documents'] != FALSE;
            break;
            case "faliment":
                $this->data['page']['pag_title'] = 'Faliment';
                $this->data['page']['pag_content'] = '';
                $this->data['page']['documents'] = $this->document->getDocuments(array('doc_type' => 'F', 'limit' => 10, 'offset' => $ppage));
                $this->data['page']['documentsCount'] = $this->document->countDocuments(array('doc_type' => 'F'));
                $this->data['page']['hasDocuments'] = $this->data['page']['documents'] != FALSE;
            break;
        }
        if ($this->data['page']['hasDocuments']) {
            foreach ($this->data['page']['documents'] as &$doc) {
                $doc['page'] = $this->page->getPages(array('pag_id' => $doc['doc_detailpage']));
                $doc['owner'] = $this->contact->getContacts(array('cnt_id' => $doc['doc_owner']));
                $doc['contact'] = $this->contact->getContacts(array('cnt_id' => $doc['doc_contact']));
            }
            $pagconf['documents'] = array('base_url' => base_url() . '/documente/' . $type,
                                          'total_rows' => $this->data['page']['documentsCount'],
                                          'per_page' => 10,
                                          'uri_segment' => 4);
            $this->pagination->initialize($pagconf['documents']);
            $this->data['page']['links'] = $this->pagination->create_links();
        } else {
            $this->data['page']['pag_content'] = 'Pagina in constructie';
        }
        $this->load->view('site_header', $this->data);
        $this->load->view('site_contenthead', $this->data);
        $this->load->view('site_contentnavi', $this->data);
        $this->load->view('site_contentnews', $this->data);
        $this->load->view('site_contentbody', $this->data);
        $this->load->view('site_contentnavi2', $this->data);
        $this->load->view('site_footer', $this->data);
    }
    
    public function licitatie($page, $ppage = 0) {
        $this->data['showNews'] = FALSE;
        $this->data['page'] = $this->page->getPages(array('pag_name' => $page));
        if ($this->data['page'] != FALSE) {
            $this->data['page']['auction'] = $this->auction->getAuctions(array('auc_detailpage' => $this->data['page']['pag_id']));
            if ($this->data['page']['auction'] == FALSE) {
                echo 'Nici o lictatie ' . $this->data['page']['pag_id'];
            }
            $this->data['page']['auction']['owner'] = $this->contact->getContacts(array('cnt_id' => $this->data['page']['auction']['auc_owner']));
            $this->data['page']['auction']['contact'] = $this->contact->getContacts(array('cnt_id' => $this->data['page']['auction']['auc_contact']));
            $this->data['page']['documents'] = $this->pagedocument->getPageDocuments(array('dp_page' => $this->data['page']['pag_id'], 'limit' => 10, 'offset' => $ppage));
            $this->data['page']['documentsCount'] = $this->pagedocument->countPageDocuments(array('dp_page' => $this->data['page']['pag_id']));
            $this->data['page']['hasDocuments'] = $this->data['page']['documents'] != FALSE;
            if ($this->data['page']['hasDocuments']) {
                $pagconf['documents'] = array('base_url' => base_url() . '/licitatie/' . $page,
                                              'total_rows' => $this->data['page']['documentsCount'],
                                              'per_page' => 10,
                                              'uri_segment' => 4);
                $this->pagination->initialize($pagconf['documents']);
                $this->data['page']['links'] = $this->pagination->create_links();
            } else {
                $this->data['page']['links'] = '';
            }
            if ($this->data['page']['auction'] != FALSE) {
            }
        } else {
            $this->data['page'] = array('pag_title' => 'Pagina solicitata nu exista. Va rugam reveniti.');
            $this->data['page']['pag_content'] = 'Pagina in constructie';
            $this->data['showNews'] = FALSE;
            $this->data['page']['hasDocuments'] = FALSE;
        }
        $this->load->view('site_header', $this->data);
        $this->load->view('site_contenthead', $this->data);
        $this->load->view('site_contentnavi', $this->data);
        $this->load->view('site_contentnews', $this->data);
        $this->load->view('site_contentauction', $this->data);
        $this->load->view('site_contentnavi2', $this->data);
        $this->load->view('site_footer', $this->data);
    }
    
    public function licitatii($type, $ppage = 0) {
        $this->data['showNews'] = FALSE;
        $this->data['page'] = array();
        $this->data['page']['hasDocuments'] = FALSE;
        switch($type) {
            case "mobile":
                $this->data['page']['hasInactiveAuctions'] = FALSE;
                $this->data['page']['pag_title'] = 'Bunuri mobile';
                $this->data['page']['pag_content'] = 'Lista bunurilor mobile';
                $this->data['page']['activeAuctions'] = $this->auction->getAuctions(array('auc_enddate >' => date('Y-m-d H:i:s'),
                                                                                          'auc_type' => 'MOBIL', 
                                                                                          'limit' => 5, 'offset' => $ppage));
                $this->data['page']['activeAuctionsCount'] = $this->auction->countAuctions(array('auc_enddate >' => date('Y-m-d H:i:s'),
                                                                                            'auc_type' => 'MOBIL'));
                $this->data['page']['hasActiveAuctions'] = $this->data['page']['activeAuctions'] != FALSE;
                if ($this->data['page']['hasActiveAuctions']) {
                    foreach ($this->data['page']['activeAuctions'] as &$auction) {
                        $auction['detail_page'] = $this->page->getPages(array('pag_id' => $auction['auc_detailpage']));
                        $auction['owner'] = $this->contact->getContacts(array('cnt_id' => $auction['auc_owner']));
                        $auction['contact'] = $this->contact->getContacts(array('cnt_id' => $auction['auc_contact']));
                    }
                    $pagconf['activeAuctions'] = array('base_url' => base_url() . '/site/licitatii/' . $type,
                                                       'total_rows' => $this->data['page']['activeAuctionsCount'], 
                                                       'per_page' => 5,
                                                       'uri_segment' => 4);
                    $this->pagination->initialize($pagconf['activeAuctions']);
                    $this->data['page']['links'] = $this->pagination->create_links();
                }
            break;
            case "imobile":
                $this->data['page']['hasInactiveAuctions'] = FALSE;
                $this->data['page']['pag_title'] = 'Bunuri imobile';
                $this->data['page']['pag_content'] = 'Lista bunurilor imobile';
                $this->data['page']['activeAuctions'] = $this->auction->getAuctions(array('auc_enddate >' => date('Y-m-d H:i:s'),
                                                                                            'auc_type' => 'IMOBIL', 
                                                                                            'limit' => 5, 'offset' => $ppage));
                $this->data['page']['activeAuctionsCount'] = $this->auction->countAuctions(array('auc_enddate >' => date('Y-m-d H:i:s'),
                                                                                            'auc_type' => 'IMOBIL'));
                $this->data['page']['hasActiveAuctions'] = $this->data['page']['activeAuctions'] != FALSE;
                if ($this->data['page']['hasActiveAuctions']) {
                    foreach ($this->data['page']['activeAuctions'] as &$auction) {
                        $auction['detail_page'] = $this->page->getPages(array('pag_id' => $auction['auc_detailpage']));
                        $auction['owner'] = $this->contact->getContacts(array('cnt_id' => $auction['auc_owner']));
                        $auction['contact'] = $this->contact->getContacts(array('cnt_id' => $auction['auc_contact']));
                    }
                    $pagconf['activeAuctions'] = array('base_url' => base_url() . '/site/licitatii/' . $type,
                                                       'total_rows' => $this->data['page']['activeAuctionsCount'], 
                                                       'per_page' => 5,
                                                       'uri_segment' => 4);
                    $this->pagination->initialize($pagconf['activeAuctions']);
                    $this->data['page']['links'] = $this->pagination->create_links();
                }
            break;
            case "istoric":
                $this->data['page']['hasActiveAuctions'] = FALSE;
                $this->data['page']['pag_title'] = 'Istoric licitatii';
                $this->data['page']['pag_content'] = '';
                $this->data['page']['inactiveAuctions'] = $this->auction->getAuctions(array('auc_enddate <' => date('Y-m-d H:i:s'), 
                                                                                            'limit' => 5, 'offset' => $ppage));
                $this->data['page']['inactiveAuctionsCount'] = $this->auction->countAuctions(array('auc_enddate <' => date('Y-m-d H:i:s')));
                $this->data['page']['hasInactiveAuctions'] = $this->data['page']['inactiveAuctions'] != FALSE;
                if ($this->data['page']['hasInactiveAuctions']) {
                    foreach ($this->data['page']['inactiveAuctions'] as &$auction) {
                        $auction['detail_page'] = $this->page->getPages(array('pag_id' => $auction['auc_detailpage']));
                        $auction['owner'] = $this->contact->getContacts(array('cnt_id' => $auction['auc_owner']));
                        $auction['contact'] = $this->contact->getContacts(array('cnt_id' => $auction['auc_contact']));
                    }
                    $pagconf['inactiveAuctions'] = array('base_url' => base_url() . '/site/licitatii/' . $type,
                                                         'total_rows' => $this->data['page']['inactiveAuctionsCount'],
                                                         'per_page' => 5, 
                                                         'uri_segment' => 4);
                    $this->pagination->initialize($pagconf['inactiveAuctions']);
                    $this->data['page']['links'] = $this->pagination->create_links();
                }
            break;
            default:
                $this->data['page'] = array('pag_title' => 'Pagina solicitata nu exista. Va rugam reveniti.');
                $this->data['page']['pag_content'] = '';
                $this->data['showNews'] = FALSE;
                $this->data['page']['hasDocuments'] = FALSE;
                $this->data['page']['hasActiveAuctions'] = FALSE;
                $this->data['page']['hasInactiveAuctions'] = FALSE;
            break;
        }
        $this->load->view('site_header', $this->data);
        $this->load->view('site_contenthead', $this->data);
        $this->load->view('site_contentnavi', $this->data);
        $this->load->view('site_contentnews', $this->data);
        $this->load->view('site_contentbody', $this->data);
        $this->load->view('site_contentnavi2', $this->data);
        $this->load->view('site_footer', $this->data);
    }
    
    public function anunturi() {
    }
    
    public function contact($todo = 'view') {
        $this->data['showNews'] = FALSE;
        $this->data['page'] = array();
        $this->data['page']['pag_title'] = 'Contact';
        if ($todo == 'view') {
            $this->data['page']['message'] = '';
            
        } else if ($todo == 'send') {
            $this->form_validation->set_message('required', 'Campul %s este obligatoriu.');
            $this->form_validation->set_message('alpha', 'Campul %s accepta doar valori alfanumerice.');
            $this->form_validation->set_message('valid_email', 'Campul %s nu contine o adresa de email valida.');
            $this->form_validation->set_rules('frmContactName', 'Nume', 'required|alpha|xss_clean');
            $this->form_validation->set_rules('frmContactEmail', 'Email', 'required|valid_email|xss_clean');
            $this->form_validation->set_rules('frmContactMessage', 'Mesaj', 'required|xss_clean');
            if ($this->form_validation->run() == FALSE) {
                $this->data['page']['message'] = validation_errors();
            } else {
                $this->email->from(set_value('frmContactEmail'), set_value('frmContactName'));
                $this->email->to('office@socieateinsolventa.ro');
                $this->email->subject('Mesaj de pe site');
                $this->email->message(set_value('frmContactMessage'));
                if ($this->email->send() != FALSE) {
                    redirect('site/contact/sent');
                } else {
                    $this->data['page']['message'] = 'Eroare la transmisia mesajului. Va rugam reveniti.';
                }
            }
        } else {
            $this->data['page']['message'] = 'Mesajul a fost trimis cu succes.';
        }
        $this->load->view('site_header', $this->data);
        $this->load->view('site_contenthead', $this->data);
        $this->load->view('site_contentnavi', $this->data);
        $this->load->view('site_contentnews', $this->data);
        $this->load->view('site_contentcontact', $this->data);
        $this->load->view('site_contentnavi2', $this->data);
        $this->load->view('site_footer', $this->data);
    }
}
